package aom.ju.ss.view.shua

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.ClassFragmentBinding
import aom.ju.ss.databinding.ClassOneBinding
import aom.ju.ss.databinding.ClassTwoBinding
import aom.ju.ss.mode.ClassMode
import aom.ju.ss.mode.ClassModeDataOne
import aom.ju.ss.mode.ClassModeDataTwo
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.utils.Fin
import aom.ju.ss.utils.VG
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.utils.show
import aom.ju.ss.view.base.BaseFragment
import aom.ju.ss.view.base.PAdapter
import com.bumptech.glide.Glide
import com.google.gson.Gson


/**
 * 刷店 ->分类
 */
class ClassFragment : BaseFragment() {
    private lateinit var binding: ClassFragmentBinding
    private lateinit var adapterClassOne: PAdapter
    private lateinit var adapterClassTwo: PAdapter
    private var classListOne: MutableList<ClassModeDataOne> = arrayListOf()
    private var ck = 0
    private var classListTwo: MutableList<ClassModeDataTwo> = arrayListOf()

    override fun init() {
        binding = viewBinding as ClassFragmentBinding
        binding.small.setEnableLoadMore(false)
        binding.classTwo.small.isEnabled = false
        binding.sx.setOnClickListener {
            binding.small.autoRefresh()
        }
        binding.small.setOnRefreshListener {
            data
        }
        data
        adapterClassOne = PAdapter(
            classListOne, R.layout.class_one
        ) { b, position ->
            val ib = b as ClassOneBinding

            ib.ck = ck == position
            ib.ckline = ck == position
            ib.nameStr = classListOne[position].title
            ib.nameTv.setOnClickListener {
                adapterClassOne.notifyItemChanged(position)
                adapterClassOne.notifyItemChanged(ck)
                ck = position
                classListTwo.clear()
                classListOne[position].child?.let {
                    classListTwo.addAll(it)
                }

                binding.classTwo.noData.VG(classListTwo.size == 0)

                adapterClassTwo.notifyDataSetChanged()
            }

        }
        binding.classOne.layoutManager = LinearLayoutManager(getActivity())
        binding.classOne.adapter = adapterClassOne

        adapterClassTwo = PAdapter(
            classListTwo, R.layout.class_two
        ) { b, position ->
            val ib = b as ClassTwoBinding
            ib.name.text = classListTwo[position].title

            Glide.with(this)
                .load(classListTwo[position].image)
                .error(R.drawable.default_img)
                .into(ib.img)
            ib.rootView.setOnClickListener {
                VideoFragment.category_id = classListOne[position].id// 设置刷店分类
                HomeShuaFragment.my?.binding?.virePager?.currentItem = 2
                VideoFragment.page = 2
                VideoFragment.my?.binding?.small?.autoRefresh()
            }
        }
        binding.classTwo.mRecycler.layoutManager = GridLayoutManager(getActivity(), 3)
        binding.classTwo.mRecycler.adapter = adapterClassTwo
    }

    val data: Unit
        get() {
            OKHttpUtils(activity, false)
                .SetApiUrl(LP_API.get_tree)
                .SetKey("api_version", "token")
                .SetValue("v3", UserInfo.token)
                .POST(this)
        }

    override fun getLay(): Int {
        return R.layout.class_fragment
    }

    companion object {
        fun newInstance(): ClassFragment {
            val fragment = ClassFragment()
            val bd = Bundle()
            fragment.arguments = bd
            return fragment
        }
    }

    override fun ok(jsonString: String?, httpTY: String?) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, ClassMode::class.java)
        binding.small.Fin()
        classListOne.clear()
        classListTwo.clear()
        mode.data?.let {
            if (it.size > 1) {
                for (a in it) {
                    a.child?.let {
                        classListOne.add(a)
                        if (classListOne.size == 1) {
                            if (a.child != null) {
                                classListTwo.addAll(a.child!!)
                            }
                        }
                    }
                }
            }
        }

        binding.classTwo.noData.VG(classListTwo.size == 0)
        binding.sx.VG(classListOne.size == 0)
        adapterClassOne.notifyDataSetChanged()
        adapterClassTwo.notifyDataSetChanged()

    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        binding.small.Fin()
        e?.show()
    }
}